package com.banshuibao.bsb_android.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.adapter.FileListAdapter;
import com.banshuibao.bsb_android.myinfterface.RecyclerItemListener;
import com.banshuibao.bsb_android.util.FileUtil;

import java.util.List;

import droidninja.filepicker.PickerManager;
import droidninja.filepicker.cursors.loadercallbacks.FileResultCallback;
import droidninja.filepicker.models.Document;
import droidninja.filepicker.models.FileType;

public class FilePickerActivity extends BaseActivity implements RecyclerItemListener<Document> {

    public final static int FILE_RESULT_CODE = 10001;

    public final static String FILE_RESULT = "file_result";

    private RecyclerView listView;

    private FileListAdapter adapter;

    private FileType fileType;

    private TextView emptyView;

    private ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file_picker);
        initData();
        initView();
    }

    private void initData() {
        String[] filters = getIntent().getStringArrayExtra("filters");
        if (filters != null && filters.length > 0)
            fileType = new FileType("", filters, 0);
        PickerManager.getInstance().addFileType(fileType);
    }

    private void initView() {
        setTitleText("选择文档");
        emptyView = (TextView) findViewById(R.id.tv_empty);
        progressBar = (ProgressBar) findViewById(R.id.progress_bar);
        listView = (RecyclerView) findViewById(R.id.listView);
        listView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new FileListAdapter(this);
        adapter.setListener(this);
        listView.setAdapter(adapter);
    }

    @Override
    protected void onStart() {
        super.onStart();
        loadData();
    }

    private void loadData() {
        FileUtil.selectFileDoc(this, new FileResultCallback<Document>() {
            @Override
            public void onResultCallback(List<Document> files) {
                if (!FilePickerActivity.this.isDestroyed()) {
                    setAdapterData(files);
                    progressBar.setVisibility(View.GONE);
                }
            }
        });
    }

    private void setAdapterData(List<Document> files) {
        if (files != null && files.size() > 0) {
            emptyView.setVisibility(View.GONE);
            listView.setVisibility(View.VISIBLE);
            if (adapter != null) {
                adapter.setData(files);
            }
        } else {
            emptyView.setVisibility(View.VISIBLE);
            listView.setVisibility(View.GONE);
        }
    }

    @Override
    public void onItemClickListener(View view, Document data) {
        Intent it = new Intent();
        it.putExtra(FILE_RESULT, data.getPath());
        setResult(RESULT_OK, it);
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (adapter != null) {
            adapter.clear();
            adapter = null;
        }
        PickerManager.getInstance().clearSelections();
    }
}
